/***
*
*  Title:配置解析接口
*
*  Description:通用配置管理器接口
*         功能：
*             1：基于“键值对”配置文件通用解析
*
*  Author:jack
*
*  Data:2023-12-28
* 
*  Modify:
*
*/

using System;
using System.Collections.Generic;
using UnityEngine;

public class ConfigManagerByJson : IConfigManager
{
    //保存键值对集合
    private static Dictionary<string, string> _AppString;

    public Dictionary<string, string> AppString
    {
        get { return _AppString; }
    }

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="jsonPath">json配置文件路径</param>
    public ConfigManagerByJson(string jsonPath)
    {
        _AppString = new Dictionary<string, string>();
        //初始化解析Json数据，加载到集合中.
        InitAndAnalysisJson(jsonPath);
    }

    /// <summary>
    /// 得到AppSetting的最大值
    /// </summary>
    /// <returns></returns>
    public int GetAppSettingMaxNumber()
    {
        if (_AppString != null && _AppString.Count >= 1)
        {
            return _AppString.Count;
        }
        else
        {
            return 0;
        }
    }

    /// <summary>
    /// 初始化解析Json数据，加载到集合中
    /// </summary>
    /// <param name="jsonPath"></param>
    private void InitAndAnalysisJson(string jsonPath)
    {
        TextAsset configInfo = null;
        KeyValuesInfo keyValuesInfo = null;
        try
        {
            configInfo = Resources.Load<TextAsset>(jsonPath);
            keyValuesInfo = JsonUtility.FromJson<KeyValuesInfo>(configInfo.text);
        }
        catch (System.Exception)
        {

            throw new Exception(GetType() + "/InitAndAnalysisJson()/jsonPath=" + jsonPath);
        }
        foreach (KeyValuesNode node in keyValuesInfo.ConfigInfo)
        {
            _AppString.Add(node.Key, node.Value);
        }
    }
}
